<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>数据编辑</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="css/mui.min.css">
		<link href="css/mui.picker.css" rel="stylesheet" />
		<link href="css/mui.poppicker.css" rel="stylesheet" />
		<!--<link rel="stylesheet" type="text/css" href="../css/mui.picker.min.css" />-->
		<style>
			.mui-btn {
				font-size: 16px;
				padding: 8px;
				margin: 3px;
			}

			h5.mui-content-padded {
				margin-left: 3px;
				margin-top: 20px !important;
			}

			h5.mui-content-padded:first-child {
				margin-top: 12px !important;
			}

			.ui-alert {
				text-align: center;
				padding: 20px 10px;
				font-size: 16px;
			}
			.wd-22{
				width: 22% !important;
			}
		</style>

	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">数据编辑</h1>
		</header>
		<div class="mui-content pd-0">
			<div class="mui-card">
				<ul class="mui-table-view">
					<li class="mui-table-view-cell">
						<div class="mui-collapse-content">
							<form class="mui-input-group">
								<div class="mui-input-row">
									<label>标签：</label>
									<div class="mui-pull-right" style="width: 65%;">
										<button id='showUserPicker' class="mui-btn mui-btn-block wd-22"
											type='button'>选</button>
										<div id='userResult' class="ui-alert" style="padding: 10px;"></div>
										<input type="hidden" id="ServicesType">
									</div>
								</div>
								<div class="mui-input-row">
									<label>值：</label>
									<input type="text" id="DataValue" placeholder="请输入值" maxlength="30">
								</div>
								<div class="mui-button-row">
									<button class="mui-btn mui-btn-primary" type="button"
										onclick="Record_Save();">保存</button>&nbsp;&nbsp;
									<button class="mui-btn mui-btn-danger" style="display: none;" id="btnDel"
										type="button" onclick="Record_Del();">删除</button>&nbsp;&nbsp;
									<button class="mui-btn" type="button"
										onclick="Record_Back()">取消</button>
								</div>
							</form>
						</div>
					</li>
				</ul>
			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/mui.picker.js"></script>
		<script src="js/mui.poppicker.js"></script>
		<script src="js/jquery.min.js"></script>
		<script src="libs/lib.js"></script>
		<script>
			var Model = {};
			var isAdd = true;

			(function($, doc) {
				mui.init({
					swipeBack: true //启用右滑关闭功能
				});

				var userPicker = new $.PopPicker();
				Page_Init();

				function Page_Init() {
					Page_Init_ddl();
					Record_Get_Info();
					if (isAdd == false)
						document.getElementById("btnDel").style.display = "";
				}

				function Page_Init_ddl() {
					Core.Post("Tag/DownBox", {}, function(result, status) {
						if (result.errorMsg) {
							Core.Msg.Alert(result.errorMsg);
							return;
						}
						//级联示例
						var cityPicker = new $.PopPicker({
							layer: 2
						});
						cityPicker.setData(result.data);
						var showCityPickerButton = doc.getElementById('showUserPicker');
						var cityResult = doc.getElementById('userResult');
						showCityPickerButton.addEventListener('tap', function(event) {
							cityPicker.show(function(items) {
								cityResult.innerText = "选择内容:" + items[0].text + " " + items[1].text;
								Model.tagId = items[1].value
								//返回 false 可以阻止选择框的关闭
								//return false;
							});
						}, false);
					});
				}
			})(mui, document);


			function Record_Get_Info() {				
				var param = Core.Tool.QueryUrlString("id");
				if (param && param.id && param.id != "undefined") {
					isAdd = false;
					Core.Post("Record/GetModel", param, function(result, status) {
						if (result.errorMsg) {
							Core.Msg.Alert(result.errorMsg);
							return;
						}
						if(!result.data)
							return;
						Model = result.data;
						document.getElementById("DataValue").value = Model.dataValue;
						document.getElementById("userResult").innerHTML = "选择:" + Model.tagtypeName + " " + Model.tagName;
					});
				}
			}

			function Record_Save() {
				var url = Model && Model.id ? "Record/Edit" : "Record/Add";
				Model.dataValue = document.getElementById("DataValue").value;
				Model.siteId = Core.Tool.GetUrlParam("siteId");
				Model.createUserId = Core.User.GetUser().id;
				if(!Model.siteId){
					Core.Msg.Alert("数组不能为空");
					return;
				}
				if(!Model.tagId){
					Core.Msg.Alert("标签不能为空");
					return;
				}
				if(!Model.dataValue){
					Core.Msg.Alert("值不能为空");
					return;
				}
				
				Core.Post(url, {
					Param: Model
				}, function(result, status) {
					if (result.errorMsg) {
						Core.Msg.Alert(result.errorMsg);
						return;
					}
					window.location.href = "record.html?siteId="+Model.siteId;
				});
			}

			function Record_Del() {
				Core.Msg.Confirm("您确定要删除该数据吗？", null, null, function(e) {
					if (e.index != 1)
						return;
					if (!Model)
						return;
					Core.Post("Record/Del", {
						id: Model.id
					}, function(result, status) {
						if (result.errorMsg) {
							Core.Msg.Alert(result.errorMsg);
							return;
						}
						window.location.href = "record.html?siteId="+Model.siteId;
					});
				})
			}
			
			function Record_Back(){
				window.location.href = "record.html?siteId=" + Model.siteId;
			}
		</script>
	</body>

</html>